package com.tbit.uqbike.object.business;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.tbit.uqbike.service.base.RedisService1;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @author MyWin E-mail:335918956@qq.com
 * @version 1.0
 * @createTime 2019-12-12 20:20
 */
@Slf4j
public class OrderHelper {
    public static final String ORDER_HEAD = "manager.order";
    public static final Integer ORDER_LIFE_SEC = 60 * 10;

    public static final Integer ORDER_RET_SUCC = 1;
    public static final Integer ORDER_RET_FAIL = 0;

    private static String getOrderReidsKey(String serNo) {
        return String.format("%s.%s", ORDER_HEAD, serNo);
    }

    public static void cacheOrderInfo(OrderInfo orderInfo, RedisService1 redisService1) {
        String key = getOrderReidsKey(orderInfo.getSerNo());
        redisService1.add(key, JSONObject.toJSONString(orderInfo), ORDER_LIFE_SEC);
    }

    public static void updateOrderSendRsp(String serNo, String rsp, RedisService1 redisService1) {
        String key = getOrderReidsKey(serNo);
        String value = redisService1.get(key);
        try {
            OrderInfo info = JSONObject.parseObject(value, OrderInfo.class);
            if (null != info) {
                log.info("key=============" + JSONObject.toJSONString(key));
                log.info("info=============" + JSONObject.toJSONString(info));
                log.info("rsp=============" + JSONObject.toJSONString(rsp));
                info.setSendRet(rsp);
                redisService1.add(key, JSONObject.toJSONString(info), ORDER_LIFE_SEC);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public static OrderInfo updateOrderRsp(String serNo, String rsp, String paramRet, RedisService1 redisService1,
                                           List<String> picList) {
        String key = getOrderReidsKey(serNo);
        String value = redisService1.get(key);
        OrderInfo info = null;
        try {
            info = JSONObject.parseObject(value, OrderInfo.class);
            if (null != info) {
                if ("1".equals(paramRet) && CollUtil.isNotEmpty(picList)) {
                    rsp = "1";
                }
                info.initRsp(serNo, rsp, paramRet);
                info.setPicList(picList);
                redisService1.add(key, JSONObject.toJSONString(info), ORDER_LIFE_SEC);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return info;
    }

    public static OrderInfo getOrderInfo(String serNo, RedisService1 redisService1) {
        String key = getOrderReidsKey(serNo);
        String value = redisService1.get(key);
        OrderInfo info = null;
        try {
            info = JSONObject.parseObject(value, OrderInfo.class);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return info;
    }
}
